package com.smart.village.big.screen.controller.bigscreen;

import com.alibaba.fastjson.JSONObject;
import com.smart.village.big.screen.domain.AjaxResult;
import com.smart.village.big.screen.service.NewsService;
import com.smart.village.big.screen.service.VideosService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.smart.village.big.screen.domain.AjaxResult.error;
import static com.smart.village.big.screen.domain.AjaxResult.success;

/**
 * 视频类
 */
@Tag(name = "视频类数据接口")
@RestController
@RequestMapping("/videos")
public class VideosController {
    @Autowired
    private VideosService videosService;
    @Autowired
    private NewsService newsService;

    @Operation(summary = "获取大屏 视频类 根据分类获取列表")
    @Parameters({
            @Parameter(name = "category", description = "视频分类"),})
    @GetMapping("/getVideosByCategory")
    @ResponseBody
    public AjaxResult getVideosByCategory(@RequestParam(required = false) String category,
                                          @RequestParam(required = false) @Parameter(description = "搜索") String search,
                                          @RequestParam(required = false) @Parameter(description = "手机号 用于获取最近的一条浏览记录") String phone,
                                          @RequestParam(required = false, defaultValue = "0") @Parameter(description = "环境标识 0 大屏 1 小程序") int environment,
                                          @RequestParam(required = false, defaultValue = "1") @Parameter(description = "当前页数") int pageNum,
                                          @RequestParam(required = false, defaultValue = "10") @Parameter(description = "每页条数") int pageSize) {
        if (ObjectUtils.isEmpty(pageNum) || pageNum == 0) {
            pageNum = 1;
        }
        if (ObjectUtils.isEmpty(pageSize) || pageSize == 0) {
            pageSize = 10;
        }
        List<JSONObject> videosByCategory = videosService.getVideosByCategory(category, search,phone, environment, pageNum, pageSize);
        return AjaxResult.success(videosByCategory);
    }

    @Operation(summary = "获取大屏 视频类 根据标签获取列表")
    @Parameters({
            @Parameter(name = "tags", description = "视频标签"),})
    @GetMapping("/getVideosByTags")
    @ResponseBody
    public AjaxResult getVideosByTags(@RequestParam(required = false) String tags,
                                      @RequestParam(required = false) @Parameter(description = "搜索") String search,
                                      @RequestParam(required = false) @Parameter(description = "手机号 用于获取最近的一条浏览记录") String phone,
                                      @RequestParam(required = false, defaultValue = "0") @Parameter(description = "环境标识 0 大屏 1 小程序") int environment,
                                      @RequestParam(required = false, defaultValue = "1") @Parameter(description = "当前页数") int pageNum,
                                      @RequestParam(required = false, defaultValue = "10") @Parameter(description = "每页条数") int pageSize) {
        if (ObjectUtils.isEmpty(pageNum) || pageNum == 0) {
            pageNum = 1;
        }
        if (ObjectUtils.isEmpty(pageSize) || pageSize == 0) {
            pageSize = 10;
        }
        List<JSONObject> videosByCategory = videosService.getVideosByTags(tags, search,phone, environment, pageNum, pageSize);
        return AjaxResult.success(videosByCategory);
    }

    @Operation(summary = "获取大屏 视频类 详情")
    @Parameters({
            @Parameter(name = "videoSerialNumber", description = "主键"),})
    @GetMapping("/getVideosDetail")
    @ResponseBody
    public AjaxResult getVideosDetail(@RequestParam String videoSerialNumber,
                                      @RequestParam(required = false) @Parameter(description = "手机号 用于获取最近的一条浏览记录") String phone) {
        return AjaxResult.success(videosService.getVideosDetail(videoSerialNumber,phone));
    }

    /**
     * 所有视频分类列表
     *
     * @return
     */
    @Operation(summary = "所有视频分类列表")
    @GetMapping("/getVideosCategoryList")
    @ResponseBody
    public AjaxResult getVideosCategoryList() {
        try {
            return success(videosService.getVideosCategoryList());
        } catch (Exception e) {
            e.printStackTrace();
            return error("程序错误请联系系统管理员");
        }
    }

    /**
     * 所有视频标签列表
     *
     * @return
     */
    @Operation(summary = "所有视频标签列表")
    @GetMapping("/getVideosTagsList")
    @ResponseBody
    public AjaxResult getVideosTagsList() {
        try {
            return success(videosService.getVideosTagsList());
        } catch (Exception e) {
            e.printStackTrace();
            return error("程序错误请联系系统管理员");
        }
    }


    /**
     * 小程序-红色观影-标签列表
     *
     * @return
     */
    @Operation(summary = "小程序-红色观影-标签列表")
    @GetMapping("/getMicroRedLookVideoTagsList")
    @ResponseBody
    public AjaxResult getMicroRedLookVideoTagsList() {
        try {
            return success(videosService.getMicroRedLookVideoTagsList());
        } catch (Exception e) {
            e.printStackTrace();
            return error("程序错误请联系系统管理员");
        }
    }

    /**
     * 小程序 获取视频内的评论列表
     *
     * @return
     */
    @Operation(summary = "小程序 获取视频内的评论列表")
    @GetMapping("/getDetailComments")
    @ResponseBody
    public AjaxResult getDiscussDetailComments(
            @RequestParam @Parameter(description = "视频id") String videoSerialNumber,
            @RequestParam(required = false, defaultValue = "1") @Parameter(description = "当前页数") int pageNum,
            @RequestParam(required = false, defaultValue = "10") @Parameter(description = "每页条数") int pageSize) {
        {
            try {
                if (ObjectUtils.isEmpty(pageNum) || pageNum == 0) {
                    pageNum = 1;
                }
                if (ObjectUtils.isEmpty(pageSize) || pageSize == 0) {
                    pageSize = 10;
                }
                return success(
                        newsService.
                                getDiscussDetailComments(videoSerialNumber, "3", pageNum, pageSize));
            } catch (Exception e) {
                e.printStackTrace();
                return error("程序错误请联系系统管理员");
            }
        }
    }

    @Operation(summary = "小程序-红色观影")
    @Parameters({
            @Parameter(name = "tags", description = "视频标签 多个使用逗号拼接"),
    })
    @GetMapping("/getMicroRedLookVideo")
    @ResponseBody
    public AjaxResult getMicroRedLookVideo(@RequestParam(required = false) String tags,
                                           @RequestParam(required = false) @Parameter(description = "手机号 用于获取最近的一条浏览记录") String phone,
                                           @RequestParam(required = false, defaultValue = "0") @Parameter(description = "环境标识 0 大屏 1 小程序") int environment,
                                           @RequestParam(required = false, defaultValue = "1") @Parameter(description = "当前页数") int pageNum,
                                           @RequestParam(required = false, defaultValue = "10") @Parameter(description = "每页条数") int pageSize) {
        if (ObjectUtils.isEmpty(pageNum) || pageNum == 0) {
            pageNum = 1;
        }
        if (ObjectUtils.isEmpty(pageSize) || pageSize == 0) {
            pageSize = 10;
        }
        JSONObject getMicroRedLookVideo = videosService.getMicroRedLookVideo(tags,phone, environment, pageNum, pageSize);
        return AjaxResult.success(getMicroRedLookVideo);
    }

}
